import React from "react";
import { Route,Redirect } from 'react-router-dom';
import { auth } from '../../../api/index'


export default class Index extends React.Component {
    componentDidUpdate () {
        if (localStorage.getItem('userInfo')) {
            auth({mid: JSON.parse(localStorage.getItem('userInfo')).id}).then(res => {
                if(res.data.code === 0) {
                    localStorage.setItem('auth', JSON.stringify(res.data.data))
                }
            })
        }
    }
   render() {
        const { location, config } = this.props;
        const { pathname } = location;
        if (!localStorage.getItem('auth')) {
            const auth = {
                has_worked: 0,
                has_activated: 0,
                has_studed: 0
            }
            localStorage.setItem('auth', JSON.stringify(auth))
        }
        const isAuth = JSON.parse(localStorage.getItem('auth'))
        
        // 初始化权利
        
        // 如果该路由不用进行权限校验，登录状态下登陆页除外
        // 因为登陆后，无法跳转到登陆页
        // 这部分代码，是为了在非登陆状态下，访问不需要权限校验的路由
        const targetRouterConfig = config.find((v) => v.path === pathname || (v.path.split('/')[1] === pathname.split('/')[1]));
        // console.log(targetRouterConfig)
        // if(targetRouterConfig && !targetRouterConfig.auth){
        //     const { component } = targetRouterConfig;
        //     return <Route exact path={pathname} component={component} />
        // }
        if (pathname === '/' && (isAuth.has_studed || isAuth.has_worked || isAuth.has_activated)) {
            return <Route exact path={pathname} component={targetRouterConfig.component} />
        } else {
            if(isAuth){
                // 如果路由合法，就跳转到相应的路由
                if(targetRouterConfig &&targetRouterConfig.auth === 'studed' &&  isAuth.has_studed){
                    return <Route path={targetRouterConfig.path} component={targetRouterConfig.component} />
                }else if(targetRouterConfig &&targetRouterConfig.auth === 'worked'&& isAuth.has_worked){
                    return <Route path={targetRouterConfig.path} component={targetRouterConfig.component} />
                }else if(targetRouterConfig &&targetRouterConfig.auth === 'activated'&& isAuth.has_activated){
                    return <Route path={targetRouterConfig.path} component={targetRouterConfig.component} />
                }else if(targetRouterConfig && !targetRouterConfig.auth){
                    // 其他合法路由
                    
                    return <Route path={targetRouterConfig.path} component={targetRouterConfig.component} />
                }else{
                    console.log(1)
                    // 如果路由不合法，重定向到 404 页面
                    return <Redirect to='/me' />
                }
            }else{
                // 非登陆状态下，当路由合法时且需要权限校验时，跳转到登陆页面，要求登陆
                if(targetRouterConfig && !targetRouterConfig.auth){
                    return <Route path={targetRouterConfig.path} component={targetRouterConfig.component} />
                }else{
                    console.log(2)
                    // 非登陆状态下，路由不合法时，重定向至 个人中心
                    return <Redirect to='/me' />
                }
            }
        }
  }
}
